package com.evil.kingdom.controller.world.city;

import com.evil.common.core.entity.R;
import com.evil.kingdom.pojo.entity.world.city.City;
import com.evil.kingdom.service.action.VectorService;
import com.evil.kingdom.service.world.city.CityService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 城池
 *
 * @author liyang
 * @date 2024-05-22 17:11
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/city")
public class CityController {

    private final CityService cityService;
    private final VectorService vectorService;

    /**
     * 初始化城池
     *
     * @return R
     */
    @RequestMapping("/init")
    public R<String> init(String suffix) {
        cityService.init(suffix);
        //List<City> cities = cityService.readJsonFile();
        //
        //List<Vector> vectors = new ArrayList<>();
        //StreamUtil.sortAndGroup(cities, City::getY, City::getX).forEach((d, list) -> {
        //    City first = null;
        //    for (City a : list) {
        //        if (null != first) {
        //            vectors.add(new Vector(first, a));
        //        }
        //        first = a;
        //    }
        //});
        //StreamUtil.sortAndGroup(cities, City::getX, City::getY).forEach((d, list) -> {
        //    City first = null;
        //    for (City a : list) {
        //        if (null != first) {
        //            vectors.add(new Vector(first, a));
        //        }
        //        first = a;
        //    }
        //});

        return R.success();
    }

    /**
     * 新增城池
     *
     * @param city city
     * @return R
     */
    @PostMapping("/add")
    public R<City> add(@RequestBody @Validated City city) {
        return R.success(cityService.add(city));
    }

    /**
     * 更新城池
     *
     * @param city city
     * @return R
     */
    @PostMapping("/update")
    public R<City> update(@RequestBody @Validated City city) {
        return R.success(cityService.update(city, null, vectorService::updateByCity));
    }

    /**
     * 查询城池
     *
     * @param name name
     * @return R
     */
    @RequestMapping("/find-by-name")
    public R<City> findByName(String name) {
        return R.success(cityService.findByName(name));
    }

    /**
     * 获取城池列表
     *
     * @return R
     */
    @RequestMapping("/list")
    public R<List<City>> list() {
        return R.success(cityService.list());
    }
}
